package com.itheima.health.controller.user;


import com.aliyuncs.exceptions.ClientException;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.SendDao;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.dto.OrderVo;
import com.itheima.health.dto.SmsLoginParam;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.Result;
import com.itheima.health.service.SendService;
import com.itheima.health.util.AliyunSmsUtils;

import com.itheima.health.util.RandomCode;
import com.itheima.health.util.SMSUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.MediaSize;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mobile")
@Slf4j
public class MobileController {

    @Autowired
    private AliyunSmsUtils aliyunSmsUtils;

    @Autowired
    private SendService sendService;
    @Autowired
    private SendDao sendDao;


    @PostMapping("/validateCode/send")
    public Result Send(String type,String telephone){
        try {
            String validateCode = RandomCode.createCode(4);
            //String validateCode="1254";
            aliyunSmsUtils.sendSms(telephone,validateCode);
            SmsLoginParam smsLoginParam = new SmsLoginParam();
            smsLoginParam.setTelephone(telephone);
            smsLoginParam.setValidateCode(validateCode);
            sendService.insert(smsLoginParam);


        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
    @PostMapping("/login/smsLogin")
    public Result smsLogin(@RequestBody SmsLoginParam smsLoginParam){
        String telephone = smsLoginParam.getTelephone();
        String validateCode = smsLoginParam.getValidateCode();
        Boolean result=sendService.check(smsLoginParam);

        if (result == false){
            return new Result(false,MessageConst.LOGIN_FAIL);
        }
        sendService.login(smsLoginParam.getTelephone());
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }
    @GetMapping("/setmeal/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> setmeals =sendService.select();
        return new Result(true,MessageConst.ACTION_SUCCESS,setmeals);
    }
    @GetMapping("/setmeal/findById")
    public Result findById(Integer id){
        Setmeal setmeal=sendService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,setmeal);
    }
    @PostMapping("/order/submit")
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam){

            Integer result=sendService.submit(orderSubmitParam);
            if (orderSubmitParam.getValidateCode()==null||orderSubmitParam.getTelephone()==null){
                return new Result(false,MessageConst.TELEPHONE_VALIDATECODE_NOTNULL);
            }
       if (result==1){
           return new Result(false,MessageConst.VALIDATECODE_ERROR);
       }
       if (result==2){
           return new Result(false,MessageConst.ORDER_FULL);
       }
       if (result ==3){
           return new Result(false,MessageConst.SELECTED_DATE_CANNOT_ORDER);
       }
       if (result ==4){
           return new Result(false,MessageConst.HAS_ORDERED);
       }else if (result ==0){
           String idCard = orderSubmitParam.getIdCard();
           Date orderDate = orderSubmitParam.getOrderDate();
           Integer memberId=sendDao.selectMemberId(idCard);
           Order order=sendDao.selectOrder(memberId,orderDate);
           return new Result(true,MessageConst.ORDERSETTING_SUCCESS,order);
       }

        return new Result(false,MessageConst.ACTION_FAIL);
    }

    @GetMapping("/order/findById")
    public Result findsById(Integer id){
        OrderVo orderVo=sendService.findOrderVoById(id);
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,orderVo);
    }


}
